function fig = fm_cpffig(varargin)
% FM_CPFFIG create GUI for Continuation Power Flow settings
%
% HANDLE = FM_CPFFIG
%       HANDLE = figure graphic handle
%
%see CPF structure for settings
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    11-Feb-2003
%Version:   1.0.2
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Settings Path CPF Theme Fig

if nargin && ischar(varargin{1})
  switch varargin{1}

   case 'hopf'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.hopf = 0;
     case 'off'
      set(gcbo,'Checked','on')
      CPF.hopf = 1;
    end

   case 'stepcut'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.stepcut = 0;
     case 'off'
      set(gcbo,'Checked','on')
      CPF.stepcut = 1;
    end

   case 'negload'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.negload = 0;
     case 'off'
      set(gcbo,'Checked','on')
      CPF.negload = 1;
      CPF.onlynegload = 0;
      set(findobj(gcf,'Tag','OptOnlyNegLoad'),'Checked','off')
      CPF.onlypqgen = 0;
      set(findobj(gcf,'Tag','OptPQGen'),'Checked','off')
      CPF.areaannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','off')
      CPF.regionannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','off')
    end

   case 'onlynegload'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.onlynegload = 0;
     case 'off'
      set(gcbo,'Checked','on')
      CPF.onlynegload = 1;
      CPF.negload = 0;
      set(findobj(gcf,'Tag','OptNegLoad'),'Checked','off')
      CPF.onlypqgen = 0;
      set(findobj(gcf,'Tag','OptPQGen'),'Checked','off')
      CPF.areaannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','off')
      CPF.regionannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','off')
    end

   case 'onlypqgen'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.onlypqgen = 0;
     case 'off'
      set(gcbo,'Checked','on')
      CPF.onlypqgen = 1;
      CPF.negload = 0;
      set(findobj(gcf,'Tag','OptNegLoad'),'Checked','off')
      CPF.onlynegload = 0;
      set(findobj(gcf,'Tag','OptOnlyNegLoad'),'Checked','off')
      CPF.areaannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','off')
      CPF.regionannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','off')
    end

   case 'ilim'

    CPF.ilim = get(gcbo,'Value');
    hdl = findobj(Fig.cpf,'Tag','PopupMenu2');
    if CPF.ilim == 0
      set(hdl,'Enable','off')
    else
      set(hdl,'Enable','on')
    end

   case 'pqgen'

    Prompt = {'Bus index','Active power [MW]','Reactive power [MW]'};
    DefAns = {'1','1','0'};
    Title = 'PQ generator data';
    NumLines = 3;
    Options.Resize='on';
    Options.WindowStyle='normal';
    Options.Interpreter='tex';
    Answer = fm_input(Prompt, Title, NumLines, DefAns,Options);
    busidx = str2num(Answer{1});
    if busidx <= 0
      fm_disp('The bus index must be positive');
      return
    end
    p = str2num(Answer{2})/Settings.mva;
    q = str2num(Answer{3})/Settings.mva;
    global Bus PQ
    idx = getint_bus(Bus,busidx);
    if idx == 0
      fm_disp('The bus index must be an existing bus');
      return
    end
    kv = getkv_bus(Bus,idx,1);
    data = [busidx Settings.mva  kv  p q  1.10   0.90  0  1];
    PQ = add_pq(PQ,data,1);
    CPF.onlypqgen = 1;
    set(findobj(Fig.cpf,'Tag','OptOnlyNegLoad'),'Checked','off')
    CPF.negload = 0;
    set(findobj(Fig.cpf,'Tag','OptNegLoad'),'Checked','off')
    CPF.onlynegload = 0;
    set(findobj(Fig.cpf,'Tag','OptPQGen'),'Checked','on')
    CPF.areaannualgrowth = 0;
    set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','off')
    CPF.regionannualgrowth = 0;
    set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','off')

   case 'areaannualgrowth'

    global Areas
    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.areaannualgrowth = 0;
     case 'off'
      CPF.onlypqgen = 0;
      set(findobj(Fig.cpf,'Tag','OptOnlyNegLoad'),'Checked','off')
      CPF.negload = 0;
      set(findobj(Fig.cpf,'Tag','OptNegLoad'),'Checked','off')
      CPF.onlynegload = 0;
      set(findobj(Fig.cpf,'Tag','OptPQGen'),'Checked','off')
      CPF.areaannualgrowth = 1;
      set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','on')
      CPF.regionannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','off')
    end

   case 'regionannualgrowth'

    global Regions
    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      CPF.regionannualgrowth = 0;
     case 'off'
      CPF.onlypqgen = 0;
      set(findobj(Fig.cpf,'Tag','OptOnlyNegLoad'),'Checked','off')
      CPF.negload = 0;
      set(findobj(Fig.cpf,'Tag','OptNegLoad'),'Checked','off')
      CPF.onlynegload = 0;
      set(findobj(Fig.cpf,'Tag','OptPQGen'),'Checked','off')
      CPF.areaannualgrowth = 0;
      set(findobj(Fig.cpf,'Tag','OptArea'),'Checked','off')
      CPF.regionannualgrowth = 1;
      set(findobj(Fig.cpf,'Tag','OptRegion'),'Checked','on')
    end
  end

  return

end

if ishandle(Fig.cpf), figure(Fig.cpf), return, end

flussi = {'Currents (I)';
          'Active Powers (P)';
          'Apparent Powers (S)'};
metodi = {'Perpendicular Intersection';
          'Local Parametrization'};
tipi   = {'Complete Nose Curve';
          'Stop at Bifurcation';
          'Stop at Limit'};

nr = 'normalized';
c2 = Theme.color02;
c3 = Theme.color03;
c4 = Theme.color04;
c5 = Theme.color05;
f1 = Theme.font01;

if strcmp(Settings.platform,'MAC')
  aligntxt = 'center';
  dm = 0.0075;
else
  aligntxt = 'left';
  dm = 0;
end

h0 = figure('Units',nr, ...
            'Color',c2, ...
            'Colormap',[], ...
            'CreateFcn', 'Fig.cpf = gcf;', ...
            'DeleteFcn', 'Fig.cpf = -1;', ...
            'MenuBar','none', ...
            'Name','PSAT-CPF', ...
            'NumberTitle','off', ...
            'PaperPosition',[18 180 576 432], ...
            'PaperUnits','points', ...
            'Position',sizefig(0.6,0.5), ...
            'Resize','on', ...
            'ToolBar','none', ...
            'FileName','fm_cpffig');
fm_set colormap

% Menu File
h1 = uimenu('Parent',h0, ...
            'Label','File', ...
            'Tag','MenuFile');
h2 = uimenu('Parent',h1, ...
            'Callback','close(gcf)', ...
            'Label','Exit', ...
            'Tag','NetSett', ...
            'Accelerator','x');

% Menu Edit
h1 = uimenu('Parent',h0, ...
            'Label','Edit', ...
            'Tag','MenuEdit');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_setting', ...
            'Label','General Settings', ...
            'Tag','ToolSett', ...
            'Accelerator','s');
% Menu Run
h1 = uimenu('Parent',h0, ...
            'Label','Run', ...
            'Tag','MenuRun');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpf(''main'');', ...
            'Label','Run CPF', ...
            'Tag','ToolCPFSett', ...
            'Accelerator','z');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_n1cont', ...
            'Label','N-1 Contingency Analysis', ...
            'Tag','ViewN1C', ...
            'Accelerator','n');
% Menu Options
h1 = uimenu('Parent',h0, ...
            'Label','Options', ...
            'Tag','MenuOpt');
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig hopf', ...
            'Label','Check for Hopf Bif.', ...
            'Tag','OptHB', ...
            'Accelerator','h');
if CPF.hopf
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig stepcut', ...
            'Label','Enforce step cut control', ...
            'Tag','OptSTC', ...
            'Accelerator','c');
if CPF.stepcut
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig negload', ...
            'Label','Include negative loads', ...
            'Tag','OptNegLoad');
if CPF.negload
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig onlynegload', ...
            'Label','Use only negative loads', ...
            'Tag','OptOnlyNegLoad');
if CPF.onlynegload
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end
h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig onlypqgen', ...
            'Label','Use only PQ generators', ...
            'Tag','OptPQGen');
if CPF.onlypqgen
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end

h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig areaannualgrowth', ...
            'Label','Use area annual growth', ...
            'Tag','OptArea');
if CPF.areaannualgrowth
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end

h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig regionannualgrowth', ...
            'Label','Use region annual growth', ...
            'Tag','OptRegion');
if CPF.regionannualgrowth
  set(h2,'Checked','on')
else
  set(h2,'Checked','off')
end

h2 = uimenu('Parent',h1, ...
            'Callback','fm_cpffig pqgen', ...
            'Label','Define single PQ generator', ...
            'Tag','OptSinglePQGen');

% Frame
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'ForegroundColor',c3, ...
               'Position',[0.025 0.275 0.95 0.675], ...
               'Style','frame', ...
               'Tag','Frame1');

% Popup Menus
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback', 'CPF.method = get(gcbo,''Value'');', ...
               'FontName', f1, ...
               'ForegroundColor',c5, ...
               'Position',[0.608  0.79  0.342  0.06], ...
               'String', metodi, ...
               'Style', 'popupmenu', ...
               'Tag','PopupMenu1', ...
               'Value',CPF.method);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback', 'CPF.flow = get(gcbo,''Value'');', ...
               'ForegroundColor',c5, ...
               'FontName', f1, ...
               'Position',[0.608 0.64  0.342  0.06], ...
               'String',flussi, ...
               'Style','popupmenu', ...
               'Tag','PopupMenu2', ...
               'Value',CPF.flow);
if CPF.ilim == 0
  set(h1,'Enable','off')
end

h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback', 'CPF.type = get(gcbo,''Value'');', ...
               'ForegroundColor',c5, ...
               'FontName', f1, ...
               'Position',[0.608 0.49  0.342  0.06], ...
               'String',tipi, ...
               'Style','popupmenu', ...
               'Tag','PopupMenu3', ...
               'Value',CPF.type);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.608  0.86  0.3  0.05], ...
               'String', 'Corrector Step Method', ...
               'Style', 'text', ...
               'Tag', 'StaticText12');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.608  0.71  0.3  0.05], ...
               'String','Flow Limits', ...
               'Style','text', ...
               'Tag','StaticText11');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.608  0.56  0.3  0.05], ...
               'String','Stop Criterium', ...
               'Style','text', ...
               'Tag','StaticText11');

% Pushbuttons
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c3, ...
               'Callback','fm_cpf(''main'');', ...
               'FontWeight','bold', ...
               'ForegroundColor',Theme.color09, ...
               'Position',[0.608  0.33  0.16  0.0882], ...
               'String','Run CPF', ...
               'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'Callback','close(gcf)', ...
               'Position',[0.794  0.33  0.16  0.0882], ...
               'String','Close', ...
               'Tag','Pushbutton2');

% Check Buttons
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'Callback', 'CPF.sbus = get(gcbo,''Value'');', ...
               'HorizontalAlignment','left',  ...
               'Position',[0.075 0.80 0.18 0.06], ...
               'Style','checkbox', ...
               'String', 'Single Slack Bus', ...
               'Tag','EditText1', ...
               'Value',CPF.sbus);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'Callback', 'CPF.vlim = get(gcbo,''Value'');', ...
               'HorizontalAlignment','left',  ...
               'Position',[0.075 0.70 0.18 0.06], ...
               'Style','checkbox', ...
               'String', 'V limit control', ...
               'Tag','EditText2', ...
               'Value',CPF.vlim);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'Callback','CPF.qlim = get(gcbo,''Value'');', ...
               'HorizontalAlignment','left', ...
               'Position',[0.075 0.60 0.18 0.06], ...
               'Style','checkbox', ...
               'String', 'Qg limit control', ...
               'Tag','EditText3', ...
               'Value',CPF.qlim);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'Callback','fm_cpffig ilim', ...
               'HorizontalAlignment','left', ...
               'Position',[0.075 0.50 0.18 0.06], ...
               'Style','checkbox', ...
               'String', 'Flow limit control', ...
               'Tag','EditDeltat', ...
               'Value',CPF.ilim);
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.linit = fval(gcbo,CPF.linit);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.075 0.33 0.18 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.linit), ...
               'Tag','EditSigma');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.075 0.38+dm 0.18 0.05], ...
               'String','Initial Lambda', ...
               'Style','text', ...
               'Tag','StaticSigma');

% Parameters for the continuation power flow routine
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.nump = fval(gcbo,CPF.nump);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.31 0.33 0.22 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.nump), ...
               'Tag','EditSigma');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.31 0.38+dm 0.22 0.05], ...
               'String','Number of Points', ...
               'Style','text', ...
               'Tag', 'StaticSigma');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.step = fval(gcbo,CPF.step);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.31 0.45 0.22 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.step), ...
               'Tag','EditGamma');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.31 0.50+dm 0.22 0.05], ...
               'String','Step Size Control', ...
               'Style','text', ...
               'Tag','StaticGamma');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.tolv = fval(gcbo,CPF.tolv);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.31 0.57 0.22 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.tolv), ...
               'Tag','EditEpsmu');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.31 0.62+dm 0.22 0.05], ...
               'String','V tolerance', ...
               'Style','text', ...
               'Tag','StatiEpsmu');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.tolf = fval(gcbo,CPF.tolf);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.31 0.69 0.22 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.tolf), ...
               'Tag','EditEps1');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.31 0.74+dm 0.22 0.05], ...
               'String','Flow tolerance', ...
               'Style','text', ...
               'Tag','StaticEps1');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c4, ...
               'Callback','CPF.tolc = fval(gcbo,CPF.tolc);', ...
               'FontName',f1, ...
               'ForegroundColor',c5, ...
               'HorizontalAlignment',aligntxt, ...
               'Position',[0.31 0.81 0.22 0.05+dm], ...
               'Style','edit', ...
               'String', num2str(CPF.tolc), ...
               'Tag','EditEps2');
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'HorizontalAlignment','left', ...
               'Position',[0.31 0.86+dm 0.22 0.05], ...
               'String','Corrector step tolerance', ...
               'Style','text', ...
               'Tag','StaticEps2');

% Banner
h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'Color',c4, ...
          'ColorOrder',Settings.color, ...
          'Layer','top', ...
          'Position',[0.025 0.025 0.1289 0.21], ...
          'Tag','Axes1', ...
          'XColor',c2, ...
          'XLim',[0.5 100.5], ...
          'XLimMode','manual', ...
          'XTick',[], ...
          'YColor',c2, ...
          'YDir','reverse', ...
          'YLim',[0.5 100.5], ...
          'YLimMode','manual', ...
          'YTick',[], ...
          'ZColor',[0 0 0]);
if Settings.hostver < 8.04
  h2 = image('Parent',h1, ...
             'CData',fm_mat('logo_psat'), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
else
  h2 = image('Parent',h1, ...
             'CData',flipud(fliplr(fm_mat('logo_psat'))), ...
             'Tag','Axes1Image1', ...
             'XData',[1 101], ...
             'YData',[1 101]);
end

h1 = axes('Parent',h0, ...
          'Box','on', ...
          'CameraUpVector',[0 1 0], ...
          'Color',c4, ...
          'ColorOrder',Settings.color, ...
          'Layer','top', ...
          'Position',[0.84 0.025 0.1289 0.21], ...
          'Tag','Axes1', ...
          'XColor',c2, ...
          'XLim',[0.5 100.5], ...
          'XLimMode','manual', ...
          'XTick',[], ...
          'YColor',c2, ...
          'YDir','reverse', ...
          'YLim',[0.5 100.5], ...
          'YLimMode','manual', ...
          'YTick',[], ...
          'ZColor',[0 0 0]);
if Settings.hostver < 8.04
  h2 = image('Parent',h1, ...
             'CData',fm_mat('logo_cpf'), ...
             'Tag','Axes1Image1', ...
             'XData',[0.5 100.5], ...
             'YData',[0.5 100.5]);
else
  h2 = image('Parent',h1, ...
             'CData',flipud(fliplr(fm_mat('logo_cpf'))), ...
             'Tag','Axes1Image1', ...
             'XData',[0.5 100.5], ...
             'YData',[0.5 100.5]);
end
h1 = uicontrol('Parent',h0, ...
               'Units', nr, ...
               'BackgroundColor',c2, ...
               'ForegroundColor', [0 1 0.5], ...
               'FontSize', 12, ...
               'FontName', 'Times', ...
               'FontWeight', 'bold', ...
               'FontAngle', 'italic',...
               'Position',[0.3 0.1 0.4 0.07], ...
               'String','Continuation Power Flow', ...
               'Style','text', ...
               'Tag','StaticText3');

if nargout > 0, fig = h0; end
